﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using QntPlatform;
using QntPlatform.Common;
using QntPlatform.Db;
using QntPlatform.Model;
using QntPlatform.Platform;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace QntSvc.Web.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SysApi : ControllerBase
    {
        ISysApi sysApi = QntPlatform.SysApi.Api;
        public int CommandRobotList(string pair, string strategyId)
        {
            var cmd = ReadRequestBody();
            if (string.IsNullOrWhiteSpace(pair) || string.IsNullOrWhiteSpace(strategyId) || string.IsNullOrWhiteSpace(cmd))
            {
                throw new ArgumentException("参数不能为空");
            }
            //InputFormatter

            var re = sysApi.CommandRobotList(pair, strategyId, cmd);
            return re;
        }
        public Result CommandRobot(int robotId, string cmd)
        {

            Log.Default().Debug($"{robotId},{cmd}", this.GetHashCode());
            if (Request.Method == "POST" && string.IsNullOrWhiteSpace(cmd))
            {
                cmd = ReadRequestBody();
            }
            var re = sysApi.CommandRobot(null, robotId, cmd);
            if (re)
            {
                return new Result() { Code = 1200 };
            }
            else
            {
                return new Result() { Code = 1400, Message = "执行失败" };
            }
        }

        private string ReadRequestBody()
        {
            string cmd;
            // Request.Body.Position = 0;
            using (var sr = new StreamReader(Request.Body))
            {
                cmd = sr.ReadToEndAsync().Result; //或者sr.ReadToEnd()
            }

            return cmd;
        }

        public RobotInfo[] GetRobotList(int offset, int length, int robotStatus, string label)
        {
            return sysApi.GetRobotList(offset, length, robotStatus, label);
        }
        public bool RestartRobot(long RobotId)
        {
            return sysApi.RestartRobot(RobotId, null);
        }

        public bool StopRobot(long RobotId)
        {
            return sysApi.StopRobot(RobotId);
        }

        public long NewRobot(RobotInfo info)
        {
            info.UserId = 1;
            return sysApi.NewRobot(info.Name, info.ExchangeId, info.Pair, info.strategyId, info.GetStrategyArgsByStr());
        }
        public StrategyInfo[] GetStrategyList()
        {
            return new StrategyStore().GetList();
        }
        protected IMng<LogInfo> logMng = new QntPlatform.Platform.MngBase<LogInfo>();
        [HttpGet]
        public Tuple<long, LogInfo[]> GetRobotLogs(long robotId, int pageNum, int pageSize)
        {
            if (robotId < 1)
                throw new ArgumentException("robotId error");
            pageNum = pageNum < 1 ? 1 : pageNum;
            pageSize = pageSize < 1 ? 1 : pageSize;
            var list = LogStore.GetDb().Select<LogInfo>().Where(p=>p.OwnerId==robotId).Count(out var total).OrderByPropertyName("Id", false).Page(pageNum, pageSize).ToList().ToArray();
            return Tuple.Create(total, list);
        }


    }
    public class RobotDto : RobotInfo
    {

    }
}
